<!doctype html>
<html>

<head>
	<title></title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
	<link rel="stylesheet" href="../res/css/weui.css" />
	<link rel="stylesheet" href="../res/css/weuix.css" />
	<link rel="stylesheet" href="../res/css/common.css" />
	<script src="../res/js/zepto.min.js"></script>
	<script src="../res/js/zepto.weui.js"></script>
	<script src="../res/js/mp_common.js?v=1"></script>
	<script src="../res/js/common.js?v=1"></script>
	<script>
		$(function () {
			strUserId = getUserId();
			order_id = getQst("order_id");
			is_order = getQst("is_order");
			id = getQst("id")
			payWay = getQst('payWay')
			// 1.9.3
			paymentMethod = getQst("paymentMethod")
			
			inside = getQst('inside')
			if(!inside || inside === 'false'){
				inside = false
			}else{
				inside = true
			}
			switchStateKey(inside)

			newGetImg(id)

			// var order_id, id;
			var userPhone;
			if (isEmpty(order_id))
				order_id = getQst("oid");
			// getorder(order_id)

			if(is_order){
				$("#saveImage").show()
				$("#btnBuyOrder").hide()
			}
			else{
				$("#saveImage").hide()
			}

			if(payWay == 'offline'){
				$("#btnBuyOrder").hide()
			}

			//医保
			$('#btnMedicare').on("click", function () {
				goPage('https://wap.360jkmall.com/#/Login?mobile=' + userPhone);
			});
			//处方流转功能
			// $('#btnBuyOrder').on("click", function () {
			//     window.open('user_dtl1.htm?prescription_id=' + order_id+ '&user_id=' + strUserId);
			// });    

			//返回上页
			$('#btnBack').on("click", function () {
				// goPage('recipe_list.htm')
				history.back()
			});

			function newGetImg(id) {
				var paramJson;
				paramJson = {
					"id": `${id}`,
					"resourceType": 4,
					"type": 2
				};
				newAjaxPost("/h5/pdf/getEstablishPdf", paramJson, newGetImgOk);
			}

			function newGetImgOk(jsonInfo) {
				if (jsonInfo.code == 1) {
					imgUrl = jsonInfo.data.url;
					$('.imgShow').attr("src", imgUrl);
					// console.log(imgUrl);
					$("#saveImage").attr("href",imgUrl);
					// if(data.inquiryMap.pay_type=='0'){
					//自费
					$('#btnMedicare').hide()
					// }else{							// }else if(data.inquiryMap.paytype==0){
					// 	//医保
					// 	$('#btnBuyOrder').hide()
					// }
					getorder(order_id)
				}
			}

			// 查询是否已创建订单
			function getorder(orderid) {
				var okFun = isHis && inside ? getHisOrderOk : getorderOk
				var paramJson = {
					"prescriptionId": id
				};
				newAjaxPost("/h5/prescriptionOrder/getIsPrescriptionOrder", paramJson, okFun);
			}

			function getorderOk(jsonInfo) {
				var data = jsonInfo.data;
				var type = getQst('type')
				Rxid = getQst("id")
				console.log(data)
				if (!Rxid) {
					Rxid = id
				}
				if (jsonInfo.code == 1 && data.orderSn != null) {
					// 有订单信息
					getindent(data.orderSn)
				} else {
					//无订单信息
					$('#btnBuyOrder').on("click", function () {
						if(hasYiBao && paymentMethod == 2){
							window.open('https://wap.qiyitx.com')
						}else{
							if(type == '5'){
								showAgreeInfo()
							}else{
								actToPage('user_dtl1.htm?prescription_id=' + order_id + '&user_id=' + strUserId + '&id=' + Rxid + '&inside=' + inside);
							}
						}
					});
				}
			}

			function getHisOrderOk(res){
				if (res.code == 1 && res.data.orderSn) {
					// 有订单信息
					// getHisOrder()
					// getindentOk(res)
					// idKey = 'id'
					$('#btnBuyOrder').text('查看订单').click(function () {
						actToPage('user_dtl5.htm?prescription_id=' + id + '&user_id=' + user_id + '&inside=' + inside)
					});
				} else {
					//无订单信息
					$('#btnBuyOrder').on("click", function () {
						actToPage('user_dtl1.htm?prescription_id=' + order_id + '&user_id=' + strUserId + '&id=' + id + '&inside=' + inside);
					});
				}
			}

			// 获取订单信息，查看是否已生成订单  非院内
			function getindent(order_id) {
				let params = {
					"order_num": order_id
				}
				idKey = 'order_sn'
				newAjaxPost("/h5/thirdParty/orderDetail", params, getindentOk);
			}

			function getindentOk(jsonInfo) {
				let stateKey 
				if(isHis && inside){
					stateKey = 'payState'
				}else{
					stateKey = 'indent_state'
				}
				var data = jsonInfo.data
				if (jsonInfo.code == '1') {
					//已生成订单，判断订单状态
					$('#btnBuyOrder').text('查看订单')
					if (data[stateKey] == STwaitPay) {
						//10:未付款(待付款)
						$('#btnBuyOrder').on("click", function () {
							actToPage('user_dtl5.htm?prescription_id=' + data[idKey] + '&user_id=' + strUserId + '&inside=' + inside);
						});
					} else if (data[stateKey] == STwaitTake) {
						//35:待自提
						$('#btnBuyOrder').on("click", function () {
							actToPage('user_dtl3.htm?prescription_id=' + data[idKey] + '&user_id=' + strUserId + '&inside=' + inside);
						});
					} else if (data[stateKey] == STcancel || data[stateKey] == STwaitSend || data[stateKey] == STsended) {
						//订单状态：0:已取消  20:待发货 30:已发货(待收货)
						$('#btnBuyOrder').on("click", function () {
							actToPage('user_dtl2.htm?prescription_id=' + data[idKey] + '&user_id=' +strUserId + '&inside=' + inside);
						});
					} else if (data[stateKey] == STfinished) {
						//40:已收货(已完成)
						$('#btnBuyOrder').on("click", function () {
							actToPage('user_dtl4.htm?prescription_id=' + data[idKey] + '&user_id=' + strUserId + '&inside=' + inside);
						});
					}
				} else {
					//未生成订单，未支付
					// 处方流转功能
					$('#btnBuyOrder').on("click", function () {
					    actToPage('user_dtl1.htm?prescription_id=' + order_id+ '&user_id=' + strUserId + '&id=' + Rxid + '&inside=' + inside);
					}); 
				}
			}

			function showAgreeInfo() {
				doGetAgree();
				$("#divAgreeInfo").popup(); //打开
				$("#agreed").click(function () {
					actToPage('user_dtl1.htm?prescription_id=' + order_id + '&user_id=' + strUserId + '&id=' + Rxid + '&inside=' + inside);
				})
			}

			function doGetAgree() {
				if (isEmpty($("#argeeTitle").text())) {
					var paramJson = {id: 5};
					newAjaxPost("/h5/utilInitData/getAgreement", paramJson, doGetAgreeOk);
				}
				return false;
			}

			function doGetAgreeOk(jsonInfo) {
				if (jsonInfo.code == 1) {
					$("#argeeTitle").text('温馨提示');
					$("#argeeContent").html(jsonInfo.data.initKey);
				}
			}
		});
	</script>
	<style>
		.weui-cell {
			padding: 0;
			margin: 0;
		}

		#btnBuyOrder {
			border-radius: 10px;
			background: #1189ff;
			color: #ffffff;
		}
		#saveImage {
			border-radius: 10px;
			background: #1189ff;
			color: #ffffff;
		}

		#btnMedicare {
			border-radius: 10px;
			border: 1px solid #1189ff;
			background: #ffffff;
			color: #1189ff;
		}

		.weui-btn:after {
			content: none;
		}

		.weui-cells {
			margin-top: 0px;
		}

		#divAgreeInfo .weui-popup__modal{
			height: 54%;
		}
		#divAgreeInfo .weui-cells_form{
			height: 100%;
		}
		.sticky-top{
			box-shadow: rgba(0, 0, 0, .3) 0 0 20px -2px;
		}
		.sticky-bottom{
			padding: 10px 50px;
			background-color: #fff;
			box-shadow: rgba(0, 0, 0, .3) 0 0 20px -2px;
		}
		.sticky-bottom .weui-btn{
			background-color: #1189ff;
			color: #fff;
			font-size: 16px;
		}
		#divAgreeInfo .weui-btn:after{
			content: " ";
		}
		.weui-cell {
			display: block;
			padding: 0 10px;
		}
		.weui-cell__bd {
			height: 40px;
			line-height: 38px;
			font-size: 15px;
		}
		strong {
			display: block;
		}

		img {
			width: 100%;
			height: 100%;
		}

		.text-gray{
			margin-top: 10px;
			text-align: center;
			font-size: 12px;
			color: rgb(250, 65, 65);
		}
	</style>

<body ontouchstart>
	<div class="weui-header weui-btn_primary">
		<div class="weui-header-left"><a id="btnBack" class="icon icon-109"></a></div>
		<div class="weui-header-title">处方详情</div>
		<div class="weui-header-right"></div>
	</div>
	<div class="weui-cells">
		<div class="weui-cell">
			<img style="width:100%" class="imgShow" />
		</div>
		<div class="text-gray">
			长按图片保存处方
		</div>
		<div style="margin: 15px 30px;">
			<a id="btnBuyOrder" href="javascript:;" class="weui-btn weui-btn_primary" style="font-size:16px;">立即购买</a>
			<a id="saveImage" href="javascript:;" class="weui-btn weui-btn_primary" style="font-size:16px;">保存图片</a>
			<!-- <a id="btnMedicare" href="javascript:;" class="weui-btn weui-btn_primary" style="font-size:16px;">医保结算</a> -->
		</div>
	</div>

	<!--用户协议-->
	<div id="divAgreeInfo" class='weui-popup__container'>
		<div class="weui-popup__overlay"></div>
		<div class="weui-popup__modal">
			<div class="weui-cells weui-cells_form" style="overflow:auto; display: flex; flex-direction: column;">
				<div style="background: #fff;" class="sticky-top">
					<div class="weui-cell__bd" style="text-align: center;"><span id="argeeTitle"></span></div>
					<div class="weui-cell__ft" style="position: absolute; right: 5px; top: 5px;">
						<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_primary close-popup">关闭</a>
					</div>
				</div>
				<div class="weui-cell" style="margin: 10px; overflow: scroll;">
					<div class="argeeContent">
						<span id="argeeContent"></span>
					</div>
				</div>
				<div class="sticky-bottom">
					<div class="weui-btn weui-btn_primary" id="agreed">已知晓并同意</div>
				</div>
			</div>
		</div>
	</div>
</body>

</html>